package com.hspedu.Writer;


import java.io.*;

/**
 * @Author Stringzhua
 * @Date 2024/3/16 20:17
 * description:BufferedReader和BufferedWriter文件拷贝
 * 1.BufferedReader和BufferedWriter是按照字符来读取操作的
 * 2.不要去操作二进制文件[声音、视频、doc、pdf],可能造成文件损坏
 */
public class BufferedCopy_ {
    public static void main(String[] args) {
        String srcFilepath = "e:\\qiyeke\\filecontroller\\story.java";
        String destFilepath = "e:\\qiyeke\\filecontroller\\story2.java";
//        String srcFilepath = "e:\\qiyeke\filecontroller\\test.mp4";
//        String destFilepath = "e:\qiyeke\filecontroller\\test2.mp4";
        BufferedReader br = null;
        BufferedWriter bw = null;
        String line;
        try {
            br = new BufferedReader(new FileReader(srcFilepath));
            bw = new BufferedWriter(new FileWriter(destFilepath));


            //readLine() 读取一行内容,并没有换行
            //读取文件写入
            while ((line = br.readLine()) != null) {
                //每一行,读取写入
                bw.write(line);
                //插入一个换行
                bw.newLine();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流
            try {
                if(br!=null){
                    br.close();
                }
                if(bw!=null){
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
